//
//  BangdingEquipUser.m
//  
//
//  Created by 谭亚明 on 2020/7/20.
//

#import "BangdingEquipUser.h"
#import "FamilyChangCell.h"
#import "TYMBaseTableView.h"
#import "Sizes.h"
#import "LYEmptyViewHeader.h"
#import "EquipmentUserModel.h"
#import "UIButton+Gradient.h"
#import "EquipmentService.h"

@interface BangdingEquipUser ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)  TYMBaseTableView *tableView;
@property(nonatomic,weak) TYMButton *subBtn;
@property(nonatomic,strong) NSMutableArray *dataArray;
@property(nonatomic,strong) NSMutableArray *seleUserArray;
@property(nonatomic,strong) EquipmentService *services;

@end

@implementation BangdingEquipUser


- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];

    [self initTableView];
    [self initBtnUI];
}

-(void)setVCtitle:(NSString*)title{
    [self addTitle:title];
}

-(void)initTableView{
    
    TYMBaseTableView *table = [[TYMBaseTableView alloc] initWithFrame:CGRectMake(0,15 , ScreenWidth, ScreenHeight - NavigateonHeight - SafeBottomMargin - 75)];
    table.separatorInset =UIEdgeInsetsMake(5,0,0, 0);
    table.delegate = self;
    table.dataSource = self;
    table.backgroundColor = viewBGColor;
    [self.view addSubview:table];
    [table registerNib:[UINib nibWithNibName:NSStringFromClass([FamilyChangCell class]) bundle:nil] forCellReuseIdentifier:[FamilyChangCell identifier]];
    self.tableView = table;
    
    NSString *titleStr;
    if ([self.equipmentUserType isEqualToString:@"00"]) {
        titleStr = @"没有可供绑定的指纹哦";
    }else if([self.equipmentUserType isEqualToString:@"01"]){
        titleStr = @"没有可供绑定的卡哦";
    }else if([self.equipmentUserType isEqualToString:@"02"]){
        titleStr = @"没有可供绑定的密码哦";
    }else if([self.equipmentUserType isEqualToString:@"03"]){
        titleStr = @"没有可供绑定的遥控哦";
    }
    self.tableView.ly_emptyView = [TYMEmptyView emptyViewWithImageStr:@"empty_img"
     titleStr:titleStr
    detailStr:@""];
}

-(void)initBtnUI{
    
    TYMButton *btn = [TYMButton btn_text:@"确定" color:rgba(255, 255, 255) font:Font_Medium(17)];
   [btn setBackgroundColor:rgba(230, 230, 230)];
    btn.frame = CGRectMake(15, ScreenHeight - 30-SafeBottomMargin-NavigateonHeight-44, ScreenWidth - 30, 44);
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 5;
    [btn addTarget:self action:@selector(confirmActin:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    self.subBtn = btn;
    [self.subBtn gradientButtonWithSize:self.subBtn.frame.size colorArray:@[(id)rgba(139, 175, 255),(id)rgba(76, 132, 255)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self getEquipmentUserList];
}

-(void)getEquipmentUserList{
    TYMWeak(self);
    [self.services getEquipmentNottheuserlistEquipmentId:self.equipmentId equipmentUserType:self.equipmentUserType userId:self.userId type:self.type completion:^(LMJBaseResponse * _Nonnull respons, NSArray<EquipmentUserModel *> * _Nonnull users) {
        if(weakself.dataArray.count){
            [weakself.dataArray removeAllObjects];
        }
        [weakself.dataArray addObjectsFromArray:users];
        [weakself.tableView reloadData];
        if (weakself.dataArray.count) {
            [weakself.tableView ly_hideEmptyView];
        }else{
            [weakself.tableView ly_showEmptyView];
        }
    }];
}



-(NSMutableArray*)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

-(NSMutableArray*)seleUserArray{
    if (_seleUserArray == nil) {
        _seleUserArray = [[NSMutableArray alloc] init];
    }
    return _seleUserArray;
}

-(EquipmentService*)services{
    if (_services == nil) {
        _services = [[EquipmentService alloc] init];
    }
    return _services;
}

-(void)confirmActin:(id)sender{
    if (!self.seleUserArray.count) {
        [WHToast showMessage:@"请选择您要绑定的用户" duration:2 finishHandler:nil];
        return;
    }
    BindAppUser *user =[[BindAppUser alloc] init];
    user.equipmentId = self.equipmentId;
    user.familyId = [UserTool getCurrentFamily].familyId;
    user.type = self.type;
    user.userId = self.userId;
    user.equipmentUserList = self.seleUserArray;
    TYMWeak(self);
    [self.services getBindappuser:user completion:^(LMJBaseResponse * _Nonnull respons) {
        if (respons.message) {
            [WHToast showMessage:respons.message duration:2 finishHandler:nil];
        }else{
            [weakself.navigationController popViewControllerAnimated:YES];
        }
    }];
}

#pragma  mark - tableView 代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [FamilyChangCell cellForHeight];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FamilyChangCell *cell = [tableView dequeueReusableCellWithIdentifier:[FamilyChangCell identifier] forIndexPath:indexPath];
    cell.equipMode = self.dataArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    EquipmentUserModel *model = self.dataArray[indexPath.row];
    model.isSele = !model.isSele;
    if (model.isSele) {
        [self.seleUserArray addObject:model];
    }else{
        [self.seleUserArray removeObject:model];
    }
    [tableView reloadData];
}

@end
